package com.dk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dk.data.Result;
import com.dk.em.ResultEnum;
import com.dk.entity.DtBrands;
import com.dk.entity.DtBuygood;
import com.dk.mapper.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("buys")
@Api(value = "省心卖")
public class BuyGoodsController {

    @Autowired
    DtBrandsMapper dtBrandsMapper;

    @Autowired
    DtModelMapper dtModelMapper;

    @Autowired
    DtGoodshsMapper dtGoodshsMapper;

    @Autowired
    DtItemsMapper dtItemsMapper;

    @Autowired
    DtBuygoodMapper dtBuygoodMapper;

    @Autowired
    JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    String from;

    @GetMapping
    public String goTo(){
        return "goto";
    }

    @GetMapping("one")
    @ApiOperation(value = "省心卖的回收大类")
    public Result one(){
        List<DtBrands> dtBrands = dtBrandsMapper.selectList(null);
        return Result.response(ResultEnum.SUCCESS).setData(dtBrands);
    }

    @GetMapping("two")
    @ApiOperation(value = "省心卖的回收大类对应的商品型号表")
    @ApiImplicitParam(name = "bid", value = "选择回收商品大类对应的分类的id", required = true, paramType = "query")
    public Result two(Integer bid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("bid",bid);
        List<DtBrands> dtBrands = dtModelMapper.selectList(queryWrapper);
        return Result.response(ResultEnum.SUCCESS).setData(dtBrands);
    }

    @GetMapping("three")
    @ApiOperation(value = "省心卖的回收大类对应的商品型号表对应的商品产品链")
    @ApiImplicitParam(name = "mid", value = "选择分类id对应的关联产品链的信息", required = true, paramType = "query")
    public Result three(Integer mid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("mid",mid);
        List<DtBrands> dtBrands = dtGoodshsMapper.selectList(queryWrapper);
        return Result.response(ResultEnum.SUCCESS).setData(dtBrands);
    }

    @GetMapping("fore")
    @ApiOperation(value = "省心卖的回收大类对应的商品型号表对应的商品产品链对应的回收商品型号规格表")
    @ApiImplicitParam(name = "gid", value = "选择产品链id对应的商品关联型号规格信息", required = true, paramType = "query")
    public Result fore(Integer gid){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.like("gid",gid);
        List<DtBrands> dtBrands = dtItemsMapper.selectList(queryWrapper);
        return Result.response(ResultEnum.SUCCESS).setData(dtBrands);
    }

    @PutMapping
    @ApiOperation(value = "成功收购")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "buyGoods", value = "接收商品json字符串格式的规格", required = true, paramType = "query"),
            @ApiImplicitParam(name = "price", value = "回收商品的价格", required = true, paramType = "query"),
            @ApiImplicitParam(name = "uid", value = "回收商品对应的用户id", required = true, paramType = "query")
    })
    public Result buyOne(@RequestParam("buyGoods") String buyGoods,@RequestParam("price") Integer price,@RequestParam("uid") Long uid){
        DtBuygood dtBuygood = new DtBuygood();
        dtBuygood.setBuyGoods(buyGoods);
        dtBuygood.setPrice(BigDecimal.valueOf(price));
        dtBuygood.setUid(uid);
        int insert = dtBuygoodMapper.insert(dtBuygood);
        String su = "商品已选择回收请关注后续人员上面工作";
        try {
            update(su);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
        return Result.response(ResultEnum.SUCCESS).setData(insert);
    }

    @PutMapping("update")
    @ApiOperation(value = "邮件")
    public void update(@RequestParam("su")String su) throws MessagingException {

        String email = "3484826196@qq.com";


        MimeMessage message = javaMailSender.createMimeMessage();
        MimeMessageHelper messageHelper = new MimeMessageHelper(message, true);
        try {
            messageHelper.setSubject("测试主题");
            messageHelper.setText("<h1>"+su+"</h1>", true);
            //收件人
            messageHelper.setTo(email);
            //发件人
            messageHelper.setFrom(from);
            javaMailSender.send(message);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

}
